package controller

import (
	"gitee.com/fierce_wolf/go-fox-edge-common/commEntity"
	"gitee.com/fierce_wolf/go-fox-edge-common/commEntityManager"
	"gitee.com/fierce_wolf/go-fox-edge-common/commRedisStatus"
	"gitee.com/fierce_wolf/go-fox-edge-common/commSql"
	"gitee.com/fierce_wolf/go-fox-edge-common/commUtil/Map"
	"gitee.com/fierce_wolf/go-fox-edge-common/edgeEntity"
	"gitee.com/fierce_wolf/go-fox-edge-common/edgeEnv"
	"gitee.com/fierce_wolf/go-fox-edge-common/httpserver"
	"go-fox-edge-kernel-manager/utils/PageUtils"
)

type dashboardManageController struct {
}

func (e *dashboardManageController) init() {
	httpserver.Server.POST("/dashboard/indicator", e.queryIndicator)
}

func (e *dashboardManageController) queryIndicator(context httpserver.Context, body map[string]interface{}) (interface{}, error) {
	indicator := make(map[string]interface{})

	e.getChannelEntityIndicator(indicator)
	e.getObjectEntityIndicator(indicator)
	e.getDeviceStatusEntityIndicator(indicator)
	e.getDeviceEntityIndicator(indicator)
	e.getServiceEntityIndicator(indicator)
	e.getEdgeIndicator(indicator)

	return indicator, nil
}

func (e *dashboardManageController) getChannelEntityIndicator(indicators map[string]interface{}) {
	channelTotal := 0
	channelType := make(map[string]interface{})
	commEntityManager.GetEntityCount(edgeEntity.Type.ChannelEntity.Type(), func(entity commEntity.IEntity) bool {
		channelEntity := entity.(*edgeEntity.ChannelEntity)
		channelTotal++
		channelType[channelEntity.ChannelType] = nil
		return true
	})

	indicators["channelTotalCount"] = channelTotal
	indicators["channelTypeCount"] = len(channelType)

}

func (e *dashboardManageController) getObjectEntityIndicator(indicators map[string]interface{}) {
	// 查询总数
	selectCount := PageUtils.MakeSelectCountSQL("tb_device_object", "")
	total, err := commSql.QueryCount(selectCount)
	if err != nil {
		return
	}

	indicators["objectTotalCount"] = total
}

func (e *dashboardManageController) getDeviceStatusEntityIndicator(indicators map[string]interface{}) {
	successCount := 0
	failedCount := 0

	commEntityManager.GetEntityCount(edgeEntity.Type.DeviceStatusEntity.Type(), func(entity commEntity.IEntity) bool {
		deviceStatusEntity := entity.(*edgeEntity.DeviceStatusEntity)

		if deviceStatusEntity.CommFailedCount > 0 {
			failedCount++
		}
		if deviceStatusEntity.CommFailedCount == 0 {
			successCount++
		}

		return true
	})

	indicators["deviceFailedCount"] = failedCount
	indicators["deviceSuccessCount"] = successCount
}

func (e *dashboardManageController) getDeviceEntityIndicator(indicators map[string]interface{}) {
	deviceTotal := 0
	deviceType := make(map[string]interface{})
	commEntityManager.GetEntityCount(edgeEntity.Type.DeviceEntity.Type(), func(entity commEntity.IEntity) bool {
		deviceEntity := entity.(*edgeEntity.DeviceEntity)

		deviceTotal++
		deviceType[deviceEntity.DeviceType] = nil

		return true
	})

	indicators["deviceTotalCount"] = deviceTotal
	indicators["deviceTypeCount"] = len(deviceType)
}

func (e *dashboardManageController) getServiceEntityIndicator(indicators map[string]interface{}) {
	resultList := commRedisStatus.GetDataList(60 * 1000)

	indicators["serviceActiveCount"] = len(resultList)
}

func (e *dashboardManageController) getEdgeIndicator(indicators map[string]interface{}) {
	data := edgeEnv.GetEdgeInfo()
	Map.Append(indicators, data)
}
